package com.project.springboot.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * description: 分页结果封装对象
 * @author 朱正磊
 * @date 2023-08-09 23:05:06
 */
@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    private long totalCount;

    /**
     * 每页记录数
     */
    private long pageSize;

    /**
     * 当前页数
     */
    private long pageNum;

    /**
     * 总页数
     */
    private long totalPage;

    /**
     * 列表数据
     */
    private List<T> list;

    /**
     * description: 分页结果构造器
     * @author 朱正磊
     * @date 2023-07-06 19:02:15
     * @param list 列表数据
     * @param totalCount 总记录数
     * @param pageSize 每页记录数
     * @param pageNum 当前页数
     */
    public PageResult(List<T> list, long totalCount, long pageSize, long pageNum) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.totalPage = (long) Math.ceil((double) totalCount / pageSize);
    }

    /**
     * description: 分页结果构造器
     * @author 朱正磊
     * @date 2023-07-06 19:00:53
     * @param page 分页模型对象
     */
    public PageResult(IPage<T> page) {
        this.list = page.getRecords();
        this.totalCount = page.getTotal();
        this.pageSize = page.getSize();
        this.pageNum = page.getCurrent();
        this.totalPage = page.getPages();
    }

    /**
     * description: 分页结果构造器
     * @author 朱正磊
     * @date 2023-07-13 11:02:08
     * @param list 列表数据
     * @param page 分页模型对象
     */
    public PageResult(List<T> list, IPage<?> page) {
        this.list = list;
        this.totalCount = page.getTotal();
        this.pageSize = page.getSize();
        this.pageNum = page.getCurrent();
        this.totalPage = page.getPages();
    }

}
